package sg.edu.nus.iss.team8s.vmcs.gui;

import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import sg.edu.nus.iss.team8s.vmcs.Listener.ActiveCustomerPanelButtonListener;
import sg.edu.nus.iss.team8s.vmcs.Listener.ActiveMachinerySimulatorPanelButtonListener;
import sg.edu.nus.iss.team8s.vmcs.Listener.ActiveMaintainerPanelButtonListener;
import sg.edu.nus.iss.team8s.vmcs.Listener.BeginSimulationButtonListener;
import sg.edu.nus.iss.team8s.vmcs.Listener.EndSimulationButtonListener;
import sg.edu.nus.iss.team8s.vmcs.commons.CommonUtility;
import sg.edu.nus.iss.team8s.vmcs.control.SimulationController;

/**
 * 
 * @author Nie XiaoYi
 *
 */

public class SimulatorControlPanel extends Frame {

	private static final long serialVersionUID = 1L;
	private Label lbl_Title;
	private Button btn_Begin;
	private Button btn_End;
	private Button btn_Customer;
	private Button btn_Maintainer;
	private Button btn_Machinery;
	private FlowLayout layout = new FlowLayout();
	
	private SimulationController simController;
	public SimulatorControlPanel(SimulationController simController){
		super();
		this.simController = simController;
		init();
		CommonUtility.alignScreenCenter(this);
	}
	
	private void init(){ 
		this.setSize(300, 385);
		this.setBackground(Color.lightGray);
		this.setResizable(false);
		this.setTitle("VMCS - Simulator Control Panel");
		layout.setVgap(0);
		layout.setHgap(0);
		this.setLayout(layout);
		this.add(getPanel());
		this.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}
	
	private Panel getPanel(){
		Font fontTitle = new Font("Dialog", Font.BOLD, 20);
		Font fontButton = new Font("Dialog", Font.BOLD, 12);
		
		Panel panel = new Panel();
		panel.setPreferredSize(new Dimension(300, 360));
		panel.setLayout(layout);
		/*layout.setVgap(0);
		layout.setHgap(0);*/
		
		lbl_Title = new Label("Simulator Control Panel");
		lbl_Title.setPreferredSize(new Dimension(300,60));
		lbl_Title.setAlignment(1);
		lbl_Title.setFont(fontTitle);
		
		btn_Begin = new Button("Begin Simlation");
		btn_Begin.setPreferredSize(new Dimension(300,60));
		btn_Begin.setFont(fontButton);
		btn_Begin.addActionListener(new BeginSimulationButtonListener(simController));
		
		btn_End = new Button("End Simulation");
		btn_End.setPreferredSize(new Dimension(300,60));
		btn_End.setFont(fontButton);
		btn_End.addActionListener(new EndSimulationButtonListener(simController));
		
		btn_Customer = new Button("Active Customer Panel");
		btn_Customer.setPreferredSize(new Dimension(300,60));
		btn_Customer.setFont(fontButton);
		btn_Customer.addActionListener(new ActiveCustomerPanelButtonListener(simController));
		
		btn_Maintainer = new Button("Active Maintainer Panel");
		btn_Maintainer.setPreferredSize(new Dimension(300,60));
		btn_Maintainer.setFont(fontButton);
		btn_Maintainer.addActionListener(new ActiveMaintainerPanelButtonListener(simController));
		
		btn_Machinery = new Button("Active Machinery Panel");
		btn_Machinery.setPreferredSize(new Dimension(300,60));
		btn_Machinery.setFont(fontButton);
		btn_Machinery.addActionListener(new ActiveMachinerySimulatorPanelButtonListener(simController));
		
		panel.add(lbl_Title);
		panel.add(btn_Begin);
		panel.add(btn_End);
		panel.add(btn_Customer);
		panel.add(btn_Maintainer);
		panel.add(btn_Machinery);
		
		return panel;
	}
	
	public void display(){
		this.setVisible(true);
	}
	
	public void setSimulationActive(boolean state){
		btn_Begin.setEnabled(!state);
		btn_End.setEnabled(state);
		btn_Customer.setEnabled(state);
		btn_Maintainer.setEnabled(state);
		btn_Machinery.setEnabled(state);
	}
	
	public void setActive(boolean state, char btnID){
		switch(btnID){
		case '1':
			btn_Customer.setEnabled(state);
			break;
		case '2':
			btn_Maintainer.setEnabled(state);
			break;
		case '3':
			btn_Machinery.setEnabled(state);
			break;
		}
	}
}
